package cn.yousuowei.huanglele.scanmine;


import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;


import android.util.Log;
import android.view.View;

import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,AdapterView.OnItemClickListener{

    // 难度列表
    private ListView levelList;
    private MainActivity context;

    private Timer timer = new Timer();
    private TimerTask task = new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    context.updateCurrentLevel();
                }
            });
        }
    };

    String[] level = new String[]{"初级","中级","高级"};
    String[] tips = new String[]{"10个雷,9x9","40个雷,16x16","100个雷,16x30"};
    int[] icons = new int[]{R.drawable.level1,R.drawable.level2,R.drawable.level3};
    // 难度数据
    private List<Map<String,Object>> level_data = new ArrayList<Map<String, Object>>();;

    // 开始按钮
    private Button startGameBtn;
    // 顶部显示当前的难度文字
    private TextView currentLevelText;
    private int current_level = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.context = this;

        levelList = (ListView) findViewById(R.id.level_list);
        ListAdapter adapter = new SimpleAdapter(context,getLevelData(),R.layout.level_list,
                new String[]{"level","tip","icon"},
                new int[]{R.id.level_list_level_name,R.id.level_list_level_tip,R.id.level_list_ico});
        levelList.setAdapter(adapter);

        levelList.setOnItemClickListener(this);

        startGameBtn = (Button) findViewById(R.id.start_game_btn);
        startGameBtn.setOnClickListener(this);

        currentLevelText = (TextView) findViewById(R.id.current_level_text);
        timer.schedule(task,500);

        adMobAd();

    }

    private void adMobAd(){
        MobileAds.initialize(getApplicationContext(), "ca-app-pub-4173758989162638~2011917300");
        AdView mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
    }
    /**
     * 列表数据
     * @return
     */
    private List<Map<String,Object>> getLevelData(){
        for (int i = 0; i < 3; i++){
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("level",level[i]);
            map.put("tip",tips[i]);
            map.put("icon",icons[i]);
            this.level_data.add(map);
        }
        return  this.level_data;
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this,MineActivity.class);
        intent.putExtra("level",this.current_level);
        startActivity(intent);
        finish();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(position != this.current_level){
            // 找到设置之前选中的为不可见
            View itemBefore = levelList.getChildAt(this.current_level);
            itemBefore.setBackgroundColor(0);
            ImageView img = (ImageView) itemBefore.findViewById(R.id.check_img);
            img.setVisibility(View.INVISIBLE);
            this.current_level = position;
            this.updateCurrentLevel();
        }
    }

    private void updateCurrentLevel(){
        currentLevelText.setText("你选择的难度是:"+level[current_level]);
        RelativeLayout view = (RelativeLayout) levelList.getChildAt(current_level);
        if(view != null){
            view.findViewById(R.id.check_img).setVisibility(View.VISIBLE);
            view.setBackgroundResource(R.color.colorCheck);
        }else {
            Log.i("levelList","没有找到");
        }
    }

    /**
     * 点击返回退出到桌面
     */
    @Override
    public void onBackPressed() {
        Intent home = new Intent(Intent.ACTION_MAIN);
        home.addCategory(Intent.CATEGORY_HOME);
        startActivity(home);
    }


}
